﻿/**
 * Created by Administrator on 2017/8/7.
 */
define(['api'],function (api) {
    var GroupMember = {
        name: 'group-member',
        template: ''
        + '<div class="c-two-right" id="displayCrew" v-cloak>'
        + '    <h3 class="c-h3">显示组员</h3>'
        + '    <div class="c-whole">'
        + '        <div class="c-switch">'
        + '            <h4 class="click-switch">'
        + '                <i-select v-model="whole" placeholder="全体销售人员">'
        + '                    <i-option v-for="item in timeWhole" :value="item.value" :key="item">'
        + '                        {{ item.label }}'
        + '                    </i-option>'
        + '                </i-select>'
        + '            </h4>'
        + '            <div class="c-switch-box">'
        + '                <Tree v-bind:data="baseData" show-checkbox multiple="true" ref="groupTree" v-on:on-check-change="onCheckChange" v-on:on-select-change="onSelectChange">'
        + '                </Tree>'
        + '            </div>'
        + '        </div>'
        + '    </div>'
        + '</div>',
        props: {
            showType: {
                type: String,
                required: true
            },
            divLayout: Boolean
        },
        data: function () {
            return {
                timeWhole: [
                    //{
                    //    value: 'bushiyong',
                    //    label: '全体'
                    //},
                    //{
                    //    value: 'lurushijian',
                    //    label: '全体销售'
                    //},
                    {
                        value: 'jihuahuifang',
                        label: '全体销售人员'
                    },
                    //{
                    //    value: 'jihuahuifangshijian',
                    //    label: '销售人员'
                    //}
                ],
                whole: '',
                selectedNodeLast: [],
                nodeData: [],
                staffData: [],
                /*显示组员*/
                baseData: [
                //    {
                //    title: '全体销售人员',
                //    value:"5",
                //    expand: true,
                //    checked: false,
                //    selected: false,
                //    isLeaf: false,
                //    children: [
                //        //#缺失部分#
                //        {
                //            title: '销售部G',
                //            value:"7",
                //            expand: false,
                //            checked: false,
                //            selected: false,
                //            isLeaf: false,
                //            children: [{
                //                title: '<span><img src="../images/touxiang01.png"><i>销售员B</i><em>客户数量：100 / 100</em><b style="display: none;"></b></span>',
                //                titleSelected: '<span><img src="../images/touxiang01.png"><i>销售员B</i><em>客户数量：100 / 100</em><b style="display: inline;"></b></span>',
                //                titleNotSelected: '<span><img src="../images/touxiang01.png"><i>销售员B</i><em>客户数量：100 / 100</em><b style="display: none;"></b></span>',
                //                value: "2",
                //                checked: false,
                //                selected: false,
                //                isLeaf: true,

                //            }, {
                //                    title: '<span><img src="../images/touxiang01.png"><i>销售员C</i><em>客户数量：100 / 100</em><b style="display: none;"></b></span>',
                //                    titleSelected: '<span><img src="../images/touxiang01.png"><i>销售员C</i><em>客户数量：100 / 100</em><b style="display: inline;"></b></span>',
                //                    titleNotSelected: '<span><img src="../images/touxiang01.png"><i>销售员C</i><em>客户数量：100 / 100</em><b style="display: none;"></b></span>',
                //                    value: "3",
                //                    checked: false,
                //                    selected: false,
                //                    isLeaf: true,
                //            }]
                //        },
                //        {
                //            title: '销售部H',
                //            value: "8",
                //            expand: false,
                //            checked: false,
                //            isLeaf: false,
                //            children: [{
                //                title: '<span><img src="../images/touxiang01.png"><i>销售员A</i><em>客户数量：100 / 100</em><b style="display: none;"></b></span>',
                //                titleSelected: '<span><img src="../images/touxiang01.png"><i>销售员A</i><em>客户数量：100 / 100</em><b style="display: inline;"></b></span>',
                //                titleNotSelected: '<span><img src="../images/touxiang01.png"><i>销售员A</i><em>客户数量：100 / 100</em><b style="display: none;"></b></span>',
                //                value: "1",
                //                checked: false,
                //                selected: false,
                //                isLeaf: true,
                //            },
                //            {
                //                title: '<span><img src="../images/touxiang01.png"><i>销售员D</i><em>客户数量：100 / 100</em><b style="display: none;"></b></span>',
                //                titleSelected: '<span><img src="../images/touxiang01.png"><i>销售员D</i><em>客户数量：100 / 100</em><b style="display: inline;"></b></span>',
                //                titleNotSelected: '<span><img src="../images/touxiang01.png"><i>销售员D</i><em>客户数量：100 / 100</em><b style="display: none;"></b></span>',
                //                value: "4",
                //                checked: false,
                //                selected: false,
                //                isLeaf: true,
                //            }]
                //        }
                //    ]
                //}
                ],
            }
        },
        methods: {
            onSelectChange: function (data) {
                this.changeData(data);
            },
            onCheckChange: function (data) {
                this.changeData(data);
            },
            changeData: function (data) {
                var i = 0;
                var selectedNode = this.$refs.groupTree.getCheckedNodes();
                for (i = 0; i < selectedNode.length; i++) {
                    selectedNode[i].checked = false;
                    selectedNode[i].selected = false;
                    if (selectedNode[i].isLeaf) {
                        selectedNode[i].title = selectedNode[i].titleNotSelected;
                    }
                }
                for (i = 0; i < this.selectedNodeLast.length; i++) {
                    this.selectedNodeLast[i].checked = false;
                    this.selectedNodeLast[i].selected = false;
                    this.selectedNodeLast[i].title = this.selectedNodeLast[i].titleNotSelected;
                }
                selectedNode = [];
                for (i = 0; i < data.length; i++) {
                    if (data[i].isLeaf) {
                        data[i].checked = true;
                        data[i].selected = true;
                        data[i].title = data[i].titleSelected;
                        selectedNode.push(data[i]);
                    }
                }
                this.$refs.groupTree.updateData();
                this.selectedNodeLast = selectedNode;
                //触发checkOrSelectChange
                window.bus.$emit("checkOrSelectChange", selectedNode);
            },
            getTreeChildren: function (data) {
                var tempChildren = [];
                for (var i = 0; i < data.length; i++){
                    var node = {
                        title: data[i].label,
                        value: data[i].value,
                        checked: false,
                        selected: false,
                        isLeaf: false,
                        children: []
                    }
                    var staff = [];
                    for (var j = 0; j < this.staffData.length; j++){
                        if (data[i].value === this.staffData[j].BelongToId) {
                            var s = {
                                title: '<span class="title' + this.staffData[j].value + '"><img src="../images/touxiang01.png"><i>' + this.staffData[j].label + '</i><em>客户数量:100/100</em><b style="display: none;"></b></span>',
                                titleSelected: '<span class="title' + this.staffData[j].value + '"><img src="../images/touxiang01.png"><i>' + this.staffData[j].label +'</i><em>客户数量:100/100</em><b style="display: inline;"></b></span>',
                                titleNotSelected: '<span class="title' + this.staffData[j].value + '"><img src="../images/touxiang01.png"><i>' + this.staffData[j].label +'</i><em>客户数量:100/100</em><b style="display: none;"></b></span>',
                                value: this.staffData[j].value,
                                checked: false,
                                selected: false,
                                //disabled:true,
                                isLeaf: true,
                            }
                            staff.push(s);
                        }
                    }
                    var dept = this.getTreeChildren(data[i].children);
                    node.children = dept.concat(staff);
                    tempChildren.push(node);
                }
                return tempChildren;
            }
        },
        mounted: function () {
            var thisGroup = this;
            api.Get_Staff_By_CompanyId({companyId: localStorage.companyId})
                .done(function (data) {
                    if (data.IsSuccess) {
                        thisGroup.staffData = data.ResultObject;

                        api.GetOrganizaitonNodes({ "companyId": localStorage.companyId })
                            .done(function (res) {
                                if (res.IsSuccess === true) {
                                    thisGroup.nodeData = res.ResultObject
                                    var c = thisGroup.getTreeChildren(thisGroup.nodeData);
                                    thisGroup.baseData = [{
                                        title: '全体销售人员',
                                        value: "all",
                                        expand: true,
                                        checked: false,
                                        selected: false,
                                        isLeaf: false,
                                        children: c,
                                    }];
                                    thisGroup.$refs.groupTree.prefixCls = "ivu-tree1";
                                }else{
                                    thisGroup.$Message.error(res.ErrMsg);
                                }
                            })
                            .fail(function () {
                                thisGroup.$Message.error('查询出错');
                            });

                    } else {
                        thisGroup.$Message.error(data.ErrMsg);
                    }
                })
                .fail(function () {
                    thisGroup.$Message.error('查询出错');
                });
        },
    };
    return GroupMember;
});
